import json

from ronglian_sms_sdk import SmsSDK

accId = '8a216da878005a800178d32fc7f74fda'
accToken = '6a04a79375e444a69556d013010744aa'
appId = '8a216da878005a800178d332b3de5000'


class SmsUtil:
    # _instance = None

    def __new__(cls, *args, **kwargs):
        # if cls._instance is None:
        #     cls._instance = super().__new__(cls, *args, **kwargs)

        if not hasattr(SmsUtil, "_instance"):  # 是否有_instance属性
            cls._instance = super().__new__(cls, *args, **kwargs)
            # 创建一个SmsSDK对象 这里只执行一次 所以SmsSDK对象只有一个
            cls._instance.sms_sdk = SmsSDK(accId, accToken, appId)

        return cls._instance

    def send_message(self, tid="1", mobile="17678335241", datas=("123456", "1")):
        # sdk = SmsSDK(accId, accToken, appId)
        # tid = '容联云通讯创建的模板'
        # mobile = '手机号1,手机号2'
        # datas = ('变量1', '变量2')

        # tid = '1'
        # mobile = '17678335241'
        # datas = ('123456', '1')
        resp = self.sms_sdk.sendMessage(tid, mobile, datas)
        resp_dict = json.loads(resp)
        # print(resp_dict)
        if resp_dict.get("statusCode") == "000000":
            print("发送短信成功")
            return 0
        else:
            print("发送短信失败")
            return 1


if __name__ == '__main__':
    SmsUtil().send_message()
